#ifndef BUILD_FUNCTIONS_H
#define BUILD_FUNCTIONS_H

#include <list>
#include "TexturedTriangle.h"
#include "Triangle.h"

using namespace std;

void buildQuad(list<Triangle*> &shapes,
               vec<float,4>    &normal,
               vec<float,4>    &topLeftPoint,     
               vec<float,4>    &topLeftColor,
               float           topLeftS,
               float           topLeftF,
               vec<float,4>   &bottomRightPoint, 
               vec<float,4>   &bottomRightColor,
               float           bottomRightS,
               float           bottomRightF,
               GLuint          texture = NULL)
{
    vec<float,4> bottomLeftPoint, topRightPoint, 
                 bottomLeftColor, topRightColor;
    Triangle* triangle;
    if (topLeftPoint[1] == bottomRightPoint[1])
    {
        bottomLeftPoint =     topLeftPoint[0], bottomRightPoint[1], bottomRightPoint[2],   1;
        topRightPoint   = bottomRightPoint[0],     topLeftPoint[1],     topLeftPoint[2],   1;
        bottomLeftColor = 0,0,0,1;
        topRightColor   = 0,0,0,1;
    }
    else
    {
        bottomLeftPoint =     topLeftPoint[0], bottomRightPoint[1],     topLeftPoint[2],   1;
        topRightPoint   = bottomRightPoint[0],     topLeftPoint[1], bottomRightPoint[2],   1;
        bottomLeftColor = 0,0,0,1;
        topRightColor   = 0,0,0,1;
    }

    if (texture != NULL)
    {
        triangle = new TexturedTriangle(
            ColoredPoint( bottomLeftPoint,  bottomLeftColor),
            ColoredPoint(bottomRightPoint, bottomRightColor),
            ColoredPoint(   topRightPoint,    topRightColor),
            normal,
            texture,
            bottomRightS,     topLeftS,  topLeftS,
            bottomRightF, bottomRightF,  topLeftF);
        shapes.push_back(triangle);
        triangle = new TexturedTriangle(
            ColoredPoint(bottomLeftPoint, bottomLeftColor),
            ColoredPoint(  topRightPoint,   topRightColor),
            ColoredPoint(   topLeftPoint,    topLeftColor),
            normal,
            texture,
            bottomRightS, topLeftS, bottomRightS,
            bottomRightF, topLeftF,     topLeftF);
        shapes.push_back(triangle);
    }
    else
    {
        triangle = new Triangle(
            ColoredPoint( bottomLeftPoint,  bottomLeftColor),
            ColoredPoint(bottomRightPoint, bottomRightColor),
            ColoredPoint(   topRightPoint,    topRightColor),
            normal);
        shapes.push_back(triangle);
        triangle = new Triangle(
            ColoredPoint(bottomLeftPoint, bottomLeftColor),
            ColoredPoint(  topRightPoint,   topRightColor),
            ColoredPoint(   topLeftPoint,    topLeftColor),
            normal);
        shapes.push_back(triangle);
    }
}


#endif